package unwdmi.connector;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

import unwdmi.db.MongoDB;
import unwdmi.dto.MeasurementDTO;
import unwdmi.main.Main;
import unwdmi.util.Validator;
import unwdmi.cache.Cache;
/**
 * This connector handles incomming data and converts them into Measurement Objects.
 * It will then check them and send them to the database
 * @author Stephan E G Veenstra
 *
 */
public class StationConnector implements Runnable {

	private Socket stationSocket;
	
	private BufferedReader stationIN;
	
	public StationConnector(Socket stationSocket) {
		
		try {
			System.out.println("A weatherServer has connected from: " + stationSocket.getInetAddress());
			this.stationSocket = stationSocket;
			stationIN = new BufferedReader(new InputStreamReader(stationSocket.getInputStream()));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run(){
		try{
			String input;
			MeasurementDTO measurement;
			
			Validator validator = new Validator();
			
			while((input = stationIN.readLine()) != null){
				input = input.replaceAll("\\s+", "");
				if(input.equals("<MEASUREMENT>")){
					measurement = new MeasurementDTO();
					
					input = stationIN.readLine();
					measurement.setSTN(Integer.parseInt(input.replaceAll("\\s*+</?\\w+>", "")));
					
					if(Main.MONGO_INSTANCE.getStation(measurement.getSTN()) != null) {
					
						input = stationIN.readLine();
						measurement.setDATE(input.replaceAll("\\s*+</?\\w+>", ""));
						
						input = stationIN.readLine();
						measurement.setTIME(input.replaceAll("\\s*+</?\\w+>", ""));
						
						input = stationIN.readLine();
						measurement.setTEMP(Double.parseDouble(input.replaceAll("\\s*+</?\\w+>", "")));
						
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setDEWP(Double.parseDouble(input));
						
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setSTP(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setSLP(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setVISIB(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setWDSP(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setPRCP(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setSNDP(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setFRSHTT(input);
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setCLDC(Double.parseDouble(input));
	
						input = stationIN.readLine().replaceAll("\\s*+</?\\w+>", "");
						if(input.length() > 0) measurement.setWNDDIR(Integer.parseInt(input));
					
						validator.validate(measurement);
						//Insert station into MongoDB
						Main.MONGO_INSTANCE.createMeasurement(measurement.getJSONString());
						System.out.println(measurement);	
					}
				}
			}
			System.err.println("Connection with the WeatherStation has been lost!");
		} catch (IOException e){
			e.printStackTrace();
		}
	}

}
